package com.example.leero.graduation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.BaseActivity;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.Util.ImageLoader;
import com.example.leero.graduation.Util.StringUtil;
import com.example.leero.graduation.adapter.IntegralAdapter;
import com.example.leero.graduation.model.IntegralRecord;
import com.example.leero.graduation.model.User;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Leero on 2017-6-4.
 */

public class IntegralActivity extends BaseActivity {

    private RecyclerView rvIntegral;
    private HeaderAndFooterWrapper headerWrapper;
    private List<IntegralRecord> dataList = new ArrayList<>();

    // head view
    private ImageView iv_avatar;
    private TextView tv_level, tv_range_integral, tv_integral;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_integral;
    }

    @Override
    protected void initView(Bundle state) {
        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("我的积分");
        findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView textRule = (TextView) findViewById(R.id.tv_run_setting);
        textRule.setText("规则");
        textRule.setVisibility(View.VISIBLE);
        textRule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(IntegralActivity.this, RuleActivity.class));
            }
        });

        setupRv();
        getIntegralRecordList();
    }

    private void setupRv() {
        rvIntegral = (RecyclerView) findViewById(R.id.rv_integral_list);
        rvIntegral.setLayoutManager(new LinearLayoutManager(this));
        headerWrapper = new HeaderAndFooterWrapper(
                new IntegralAdapter(this, R.layout.item_integral_record, dataList));
        View v = LayoutInflater.from(this).inflate(R.layout.fragment_present_exp_header, rvIntegral, false);
        headerWrapper.addHeaderView(v);
        rvIntegral.setAdapter(headerWrapper);

        initHeader(v);
    }

    private void initHeader(View v) {
        iv_avatar = (ImageView) v.findViewById(R.id.iv_avatar);
        tv_level = (TextView) v.findViewById(R.id.tv_level);
        tv_range_integral = (TextView) v.findViewById(R.id.tv_range_integral);
        tv_integral = (TextView) v.findViewById(R.id.tv_integral);

        User user = App.getLoginUser();
        if (!StringUtil.isEmpty(user.user_icon)) {
            ImageLoader.load(this, iv_avatar, user.user_icon);
        } else {
            iv_avatar.setImageResource(R.drawable.default_avatar_cricle);
        }
        tv_integral.setText("当前积分: " + user.integral);
        tv_level.setText("运动员等级"+getIntegralLevel(user.integral)+"级");
        if (getIntegralLevel(user.integral) == 10){
            tv_range_integral.setText("您已满级");
        }
        tv_range_integral.setText("距离下一级还须"+getRangeIntegral(user.integral)+"积分");
    }

    private void getIntegralRecordList() {
        OkHttpUtils.post().url(API.INTEGRAL_RECORD).addParams("user_id", App.getLoginUser().id+"")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                dataList.clear();
                dataList.addAll(GsonUtil.jsonToArrayList(GsonUtil.getData(response), IntegralRecord.class));
                headerWrapper.notifyDataSetChanged();
            }
        });
    }

    private int getIntegralLevel(int integral) {
        if (integral <= 50) return 1;
        if (integral >= 51 && integral <= 110) return 2;
        if (integral >= 111 && integral <= 170) return 3;
        if (integral >= 171 && integral <= 240) return 4;
        if (integral >= 241 && integral <= 320) return 5;
        if (integral >= 321 && integral <= 410) return 6;
        if (integral >= 411 && integral <= 510) return 7;
        if (integral >= 511 && integral <= 650) return 8;
        if (integral >= 651 && integral <= 800) return 9;
        if (integral >= 801) return 10;
        return 0;
    }

    private String getRangeIntegral(int integral) {
        switch (getIntegralLevel(integral)) {
            case 1: return (51-integral) + "";
            case 2: return (111-integral) + "";
            case 3: return (171-integral) + "";
            case 4: return (241-integral) + "";
            case 5: return (321-integral) + "";
            case 6: return (411-integral) + "";
            case 7: return (511-integral) + "";
            case 8: return (651-integral) + "";
            case 9: return (801-integral) + "";
        }
        return "";
    }
}
